//
//  JLikeView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/31.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JLikeView.h"
#import "JBaseCollectionViewCell.h"
#import "UICollectionView+JCollectionView.h"

@interface JLikeView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic , assign) CGFloat cellWidth;

@end

@implementation JLikeView
// 注意const的位置
static NSString *const cellId1 = @"cellId1";


- (instancetype)initWithFrame:(CGRect)frame isHeadView:(BOOL)isHead
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        _isHeadView = isHead;
        _dataArr = [NSArray array];
        _cellWidth = (kWindowW - 15*3)/2.0;
        
        if (_isHeadView) {
            [self setUpView];
        }
        
        [self setUpCollectView];
    }
    return  self;
}

- (void)setUpView {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 40)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    _topView = view;
    
    YYLabel *titleLabel = [YYLabel new];
    titleLabel.left = 0;
    titleLabel.top = 10;
    titleLabel.size = CGSizeMake(kWindowW, 30);
    titleLabel.text = @"猜你喜欢";
    [titleLabel sizeToFit];
    titleLabel.top = 17;
    titleLabel.centerX = self.centerX;
    titleLabel.size = CGSizeMake(titleLabel.width, titleLabel.height);
    [view addSubview:titleLabel];
    
    CALayer *imgLayer = [CALayer layer];
    imgLayer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"likely"].CGImage);
    imgLayer.centerX = titleLabel.centerX  -  70;
    imgLayer.top = 15;
    imgLayer.size = CGSizeMake(20, 20);
    [view.layer addSublayer:imgLayer];
}

- (void)setIsHeadView:(BOOL)isHeadView {
    _isHeadView = isHeadView;
}

- (void)setUpCollectView {
    if (_isHeadView) {
        _collectView = [UICollectionView initWithCollectionView:CGRectMake(0, 60, kWindowW, self.height) withDelegate:self];
        _collectView.scrollEnabled = NO;
    }
    else{
        _collectView = [UICollectionView initWithCollectionView:CGRectMake(0, 0, kWindowW, self.height) withDelegate:self];
        _collectView.scrollEnabled = YES;
        self.collectView.alwaysBounceVertical = YES;
        _collectView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            if (self.dataArr.count == _pageModel.pageIndex * _pageModel.pageSize || self.dataArr.count == 0) {
                if (_refreshCellBlock) {
                    _refreshCellBlock(_pageModel.pageIndex);
                }
                [_collectView.mj_footer endRefreshing];
            }
            else
            {
                [_collectView.mj_footer endRefreshingWithNoMoreData];
            }
            
        }];
        
        [_collectView.mj_footer beginRefreshing];

    }
    

    
    //    if (_isHeadView) {
//    [self.collectView registerClass:[JCollectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headId];
    //    }
    
    // 注册cell
    [self.collectView registerClass:[JBaseCollectionViewCell class] forCellWithReuseIdentifier:cellId1];
    
    [self addSubview:_collectView];
}

//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 1;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JBaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId1 forIndexPath:indexPath];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        cell.prodeutModel = _dataArr[indexPath.row];
    });
    
    return cell;
}

//// 和UITableView类似，UICollectionView也可设置段头段尾
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    //    if (_isHeadView) {
//    if([kind isEqualToString:UICollectionElementKindSectionHeader])
//    {
//        JCollectionHeadView *headerView = [_collectView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headId forIndexPath:indexPath];
//        
//        return headerView;
//    }
//    //    }
//    
//    return nil;
//}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
    
}


#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return (CGSize){_cellWidth,260};
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15.f;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return (CGSize){kWindowW,230 + kWindowW /2.0 + 20 + 40};
//}


//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return (CGSize){kWindowW,22};
//}




#pragma mark ---- UICollectionViewDelegate

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//// 点击高亮
//- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//}

// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cellSelectBlock) {
        _cellSelectBlock(_dataArr[indexPath.row]);
    }
}


//// 长按某item，弹出copy和paste的菜单
//- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}

//// 使copy和paste有效
//- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
//{
//    if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"])
//    {
//        return YES;
//    }
//
//    return NO;
//}
//
////
//- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
//{
//    if([NSStringFromSelector(action) isEqualToString:@"copy:"])
//    {
//        //        NSLog(@"-------------执行拷贝-------------");
//        [_collectView performBatchUpdates:^{
////            [_section0Array removeObjectAtIndex:indexPath.row];
//            [_collectView deleteItemsAtIndexPaths:@[indexPath]];
//        } completion:nil];
//    }
//    else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
//    {
//        NSLog(@"-------------执行粘贴-------------");
//    }
//}


- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    if (dataArr.count == 0) {
        _topView.hidden = YES;
    }
    _collectView.frame = CGRectMake(0, 40, kWindowW, ceilf(_dataArr.count/2.0) * 300);
    [_collectView reloadData];
}

- (CGFloat)getViewHeight
{
    if (_dataArr.count == 0) {
        return 0;
    }
    else
    {
        CGFloat height = 40 +  _collectView.height ;
        return height;
    }
    
    return 0;
}

@end
